/*
 * TestGAAnimator.java
 *
 * Created on December 5, 2007, 1:01 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package simfinal;

import com.jme.app.SimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Controller;
import com.jme.scene.TriMesh;
import com.jme.scene.shape.Cone;

/**
 *
 * @author Eric Drucker
 */
public class TestGAAnimator extends SimpleGame
{
	
	Cone [] rocketObjects = new Cone[FinalDATA.POPSIZE];
	Rocket []  rockets = new Rocket[FinalDATA.POPSIZE];
	
	FinalPopulation population = new FinalPopulation();
	
	/** Creates a new instance of TestGAAnimator */
	public TestGAAnimator()
	{
	}
	
	
	public static void main(String[] args)
	{
		TestGAAnimator app = new TestGAAnimator(); // Create Object
		// Signal to show properties dialog
		app.setDialogBehaviour(SimpleGame.ALWAYS_SHOW_PROPS_DIALOG);
		app.start(); // Start the program
	}

	protected void simpleInitGame()
	{
		//create rockets
		for(int i = 0; i < rocketObjects.length; i++)
		{
			rocketObjects[i] = new Cone("rocketObject" + i,10,10,.1f,1);
			rocketObjects[i].setLocalTranslation(100, 100, -100);
			///set bounding box
			rocketObjects[i].setModelBound(new BoundingBox());
			rocketObjects[i].updateModelBound();
			//set color
			rocketObjects[i].setSolidColor(ColorRGBA.cyan);
			//attach them to root node
			rootNode.attachChild(rocketObjects[i]);
			
			rocketObjects[i].addController(new RocketMover(rocketObjects[i],i));
		}
		
		population = new FinalPopulation();
		FinalChromosome[] individuals = population.getIndividuals();
		
		for(int i = 0; i < rockets.length; i++)
		{
			rockets[i] = new Rocket(individuals[i].getAlleles(), new Vector(0,0,0));
		}
		
		
		
		rootNode.addController(new Evolver());
	}
	
	
	class Evolver extends Controller
	{
		public Evolver()
		{
		}
		
		public void update(float time)
		{
			boolean allEmpty = true;
			for(int j = 0; j < rockets.length; j++)
			{
				if(!rockets[j].isEmpty())
				{
					allEmpty = false;
				}
			}
			
			if(allEmpty)
			{
				FinalChromosome[] individuals = population.getIndividuals();
				//set fitnesses
				for(int i = 0; i < individuals.length; i++)
				{
					individuals[i].setFitness(rocketObjects[i].getLocalTranslation().y);
					
	System.out.println("BEST FITNESS 2: " + rocketObjects[i].getLocalTranslation().y);
				}
				
				//evolve population
				population.evolve();
				
				//reset rockets
				for(int i = 0; i < rocketObjects.length; i++)
				{
					rocketObjects[i].setLocalTranslation(0,0,0);
				}
				
				
				
				
				FinalChromosome[] newIndividuals = population.getIndividuals();
				//create rockets with new strings
				for(int i = 0; i < rockets.length; i++)
				{
					rockets[i] = new Rocket(newIndividuals[i].getAlleles(), new Vector(0,0,0));
				}
				//restart animation
				
			}
		}
		
	}
	
	
	class RocketMover extends Controller
	{
		TriMesh shape;
		int rocketNum;
			
		public RocketMover(TriMesh shape, int rocketNum)
		{
			this.shape = shape;
			this.rocketNum = rocketNum;
		}

		public void update(float time)
		{
			Quaternion q2 = new Quaternion();
			rockets[rocketNum].Fire();
				shape.setLocalTranslation(rockets[rocketNum].x0dot.toVector3f().mult(100f));
			q2.fromAngleAxis(rockets[rocketNum].getRotation(), new Vector3f(1,0,0));           
			shape.setLocalRotation(q2);
		}
		
		
	}
	
}
